<?php
namespace app\Console;

use think\console\Command;
use think\console\Input;
use think\console\Output;

//异步多进程curl 呵呵
class HttpClient extends Command
{
    // 命令行配置函数
    protected function configure()
    {
        // setName 设置命令行名称
        // setDescription 设置命令行描述
        $this->setName('http:client')->setDescription('Start Http Client!');
    }

    // 设置命令返回信息
    protected function execute(Input $input, Output $output)
    {
        // 将域名解析为IP地址
        \swoole_async_dns_lookup("www.zwdu.com", function ($domainName, $ip) { //https不行啊
            // 实例化 swoole_http_client
            $cli = new \swoole_http_client($ip, 443);
            // 设置 Http 请求头
            $cli->setHeaders([
                'Host'            => $domainName,
                //"User-Agent"      => 'Chrome/49.0.2587.3',
                "User-Agent"      => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11',
                'Accept'          => 'text/html,application/xhtml+xml,application/xml',
                'Accept-Encoding' => 'gzip',
            ]);
            // 发起GET请求
            $cli->get('/', function ($cli) {

                // 统计返回内容长度
                echo "Length: " . strlen($cli->body) . "\n";
                // 显示返回内容
                echo $cli->body;

                // 关闭请求
                $cli->close();
            });
        });
        $output->writeln("HttpClient: Start.\n");
    }
}